package edu.toronto.whimper;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.*;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import edu.toronto.whimper.DataProcessor.DBAdapter;
import edu.toronto.whimper.DataProcessor.DBManager;
import edu.toronto.whimper.GeneralObjects.NoiseSample;
import edu.toronto.whimper.GeneralObjects.Range;
import edu.toronto.whimper.MapObjects.MapItemizedOverlay;
import edu.toronto.whimper.MapObjects.MapOverlay;

public class NoiseMap extends MapActivity {
	
	private MapView mapView;
	private TextView hoursText;
	//private MapController mc;
	//private GeoPoint p;
    private List<Overlay> mapOverlays;
    private Drawable drawable;
    private MapItemizedOverlay itemizedoverlay;
    private MapOverlay noiseMapOverlay;
    
    private DBAdapter db;
    private ArrayList<NoiseSample> allSamples;
    private ArrayList<NoiseSample> rangeSamples;
    private Range range = new Range(0, 0);
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.noisemap);  
        
        setButtonsWidth();
        
        hoursText = (TextView) findViewById(R.id.hoursValue);
        //get the mapView
        mapView = (MapView) findViewById(R.id.mapview);           
        //by default set the map mode
        //mapView.setSatellite(true);
        mapView.setSatellite(false);
        mapView.setStreetView(true);
        mapView.setBuiltInZoomControls(true); 
        
        //getting the overlays list
        mapOverlays = mapView.getOverlays();
        //configuration by default - green marker
        drawable = this.getResources().getDrawable(R.drawable.red_dot);     
        ////itemizedoverlay = new MapItemizedOverlay(drawable, this);        
        db = new DBAdapter(this); 
        
        //set markers
        ////setAllRecordedSamplesMarkers();
        
        ////mapOverlays.add(itemizedoverlay);
        
        noiseMapOverlay = new MapOverlay();
        allSamples = DBManager.getAllNoiseSamples(db);
        noiseMapOverlay.setNoiseSamples(allSamples);
        mapOverlays.add(noiseMapOverlay);
        focusMap();
      
                       
        }
	
	@Override
	protected boolean isRouteDisplayed()
	{
	    return false;
	}
	
	private void focusMap()
	{
        if (allSamples.size()>0)
        {
        	NoiseSample lastSample;
        	lastSample = allSamples.get(allSamples.size()-1);
        	
    		GeoPoint lastRecordedPoint;
    		lastRecordedPoint = new GeoPoint((int)(lastSample.getLocation().getLatitude()* 1E6), (int)(lastSample.getLocation().getLongitude()* 1E6));
        	zoomLocation(lastRecordedPoint);
        }
        else
        {
        	// Center at Bahen building
        	zoomLocation(new GeoPoint(43659221, -79396477));
        }
	}
	
		
    public void hoursButtonClickHandler(View clickedButton)
    {
    	int count = 0;
    	String text = "";
    	String hoursRange = "24 hours map";
    	boolean all = false;
 
    	switch(clickedButton.getId())
    	{
    	case R.id.btnMorning: //6 - 12    		
    		range = new Range(6,12);
    		text = "morning"; 
    		hoursRange = "6 AM to 12 PM";
    		break;
    	case R.id.btnAfternoon: //12 - 18   		
    		range = new Range(12, 18);
    		text = "afternoon"; 
    		hoursRange = "12 PM to 6 PM";    		
    		break;
    	case R.id.btnNight: //18 - 24
    		range = new Range(18,24);
    		text = "night"; 
    		hoursRange = "6 PM to 12 PM";
    		break;	
    	case R.id.btnLateNight:    		
    		range = new Range(0,6);
    		text = "late night"; 
    		hoursRange = "12 AM to 6 AM";	
    		break;
        case R.id.btnAllHours:    		
        	all = true;    		
    		break;
    	}
    	
    	if(!all)
    	{
    		rangeSamples = DBManager.getNoiseSampleByRange(allSamples, range);
    		count = rangeSamples.size();
    		if(count>0)
    		{
    			Toast.makeText(this, "There are "+ Integer.toString(count)+ " samples.", Toast.LENGTH_SHORT).show();        		       		
    		}
    		else
    		{
    			Toast.makeText(this, "There are no samples in the " + text, Toast.LENGTH_SHORT).show();
    		}
        	
    	}
    	if(!mapOverlays.isEmpty()) //erasing the previous overlay and adding the new one
		{ 
		     mapOverlays.clear();
		     if(!all)
		     {
		       noiseMapOverlay.setNoiseSamples(rangeSamples);
		     }
		     else
		     {
		    	noiseMapOverlay.setNoiseSamples(allSamples);		    	 
		     }
 			 mapOverlays.add(noiseMapOverlay);
 		     focusMap();
		     //mapView.invalidate();
		}
    	hoursText.setText(hoursRange);
    }
	
	private void setAllRecordedSamplesMarkers()
	{
        allSamples = DBManager.getAllNoiseSamples(db);
        
        int count = allSamples.size(); 
        GeoPoint p;
    	
        if( count > 0)
        {
	    	for (NoiseSample sample : allSamples) 
	    	{
	    		count = count -1;
	    		int latE6 =  (int)(sample.getLocation().getLatitude()* 1E6);
	    		int longE6 = (int)(sample.getLocation().getLongitude()* 1E6);
	    		double db = sample.getNoise();
	    		
	    		p = new GeoPoint(latE6, longE6);
		        showMarker(p,db);	
		        
		        if(count == 0 && p.getLatitudeE6()!=0){    	
		        	zoomLocation(p);
		        	}
		        /* zooming the location of the last sample,assuming that the user is located there*/
	    	}
        }
        else 
        {
        	Toast.makeText( getApplicationContext(), "There are no sound samples recorded", Toast.LENGTH_LONG).show();
        }
        //mapOverlays.add(itemizedoverlay);
	    
	}
	
	private void showMarker(GeoPoint p, double noise )
	{
		//getting the correct marker to show
		//setColor(noise);
		//drawable = getMarker();  
		//itemizedoverlay = new MapItemizedOverlay(drawable, this);
		
		//adding this location to the overlay array    
	    OverlayItem overlayitem = new OverlayItem(p, "Recorded noise in db: ", Double.toString(noise));	        
	    itemizedoverlay.addOverlay(overlayitem);  
	    
	}
	private void zoomLocation(GeoPoint p)
	{
		MapController mc;
		 mc = mapView.getController();
		 //mc.animateTo(p);
		 mc.setCenter(p);
	     mc.setZoom(17); 
	     mapView.invalidate();
	}
		
	private void setButtonsWidth()
    {
    	//getting the metrics
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //int h = metrics.heightPixels;
        int w = metrics.widthPixels;
        //setting the width of the buttons
        Button btnMorning = (Button) findViewById(R.id.btnMorning);
        btnMorning.setWidth(w/5);
        Button btnAfternoon = (Button) findViewById(R.id.btnAfternoon);
        btnAfternoon.setWidth(w/5);
        Button btnNight = (Button) findViewById(R.id.btnNight);
        btnNight.setWidth(w/5);
        Button btnLateNight = (Button) findViewById(R.id.btnLateNight);
        btnLateNight.setWidth(w/5);
        Button btnAll = (Button) findViewById(R.id.btnAllHours);
        btnAll.setWidth(w/5);
    	
    }
    
    

}
